/*
@author: zwz
@file: 03.三乐购物系统.go
@time: 2022/11/20 0020 15:21:06
*/
package main

import (
	"bufio"
	"encoding/json"
	"fmt"
	"os"
	"strconv"
	"strings"
)

//直接申明map类型，没有赋值，指向nil
//var user2 map[string]string
var user = make(map[string]map[string]interface{})
var user_file string = "项目/03.三乐购物系统/user.txt"
var products = map[string]map[string]interface{}{
	"F001":{"name":"apple", "price":9},
	"F002":{"name":"banana", "price":3},
	"F003":{"name":"orange", "price":4},
}
var car = make(map[string][][]string)

func main() {
	//存放用户信息
	// {"sc":{"passwd":"123456", "money":"300"}}
	//var user map[string]map[string]interface{}
	//存放商品信息
	// {"F001":{"name":"apple", "price":"9"},
	//	"F002":{"name":"banana", "price":"3"}}
	//var products = map[string]map[string]interface{}{
	//	"F001":{"name":"apple", "price":9},
	//	"F002":{"name":"banana", "price":3},
	//	"F003":{"name":"orange", "price":4},
	//}

	fmt.Println("********欢迎进入购物系统*********")

	flag := 0
	for {
		if flag == 1 {
			break
		}
		var op string
		var name string
		var passwd string
		var options string
		var product_id string
		var num string
		fmt.Println("1、登录")
		fmt.Println("2、注册")
		fmt.Print("请输入你的操作：")
		fmt.Scan(&op)
		switch op {
		case "1":
			fmt.Print("请输入用户名：")
			fmt.Scanln(&name)
			fmt.Print("请输入密码：")
			fmt.Scanln(&passwd)
			result := login(name, passwd)
			if result {
				fmt.Println("登录成功")
				for true {
					menu()
					fmt.Println("请输入你的选择：")
					fmt.Scanln(&options)
					if options == "1" {
						show_goods()
					}else if options == "2" {
						fmt.Println("请输入要购买商品的编号：")
						fmt.Scanln(&product_id)
						if _, ok := products[product_id]; ok {
							fmt.Println("请输入购买数量：")
							fmt.Scanln(&num)
							add_car(name, product_id, num)
						}
					}else if options == "3" {
						checkout(name)
					}else if options == "4" {
						break
					}else {
						fmt.Println("请输入正确的选择！")
					}
				}
			}else {
				fmt.Println("登录失败")
			}
		case "2":
			fmt.Print("请输入用户名：")
			Read := bufio.NewReader(os.Stdin)
			name, _ := Read.ReadString('\n')
			fmt.Print("请输入密码：")
			passRead := bufio.NewReader(os.Stdin)
			passwd, _ := passRead.ReadString('\n')
			result := register(strings.Replace(name, "\n", "", -1), strings.Replace(passwd, "\n", "", -1))
			if result {
				fmt.Println(user)
			}
		case "q":
			flag = 1
		}
	}



}

//从文件中读取信息 初始化user
func init_user(filename string)  {
	content, err := os.ReadFile(filename) //只读方式打开文件
	if err != nil {
		fmt.Println("初始化用户信息失败，没有指定文件")
	}else {
		err1 := json.Unmarshal(content, &user)
		if err1 != nil {
			fmt.Println("json格式化失败，初始化用户信息失败！")
		}
	}
}

//登录
func login(name string, passwd string) bool {
	init_user(user_file)
	if _, ok := user[name]; ok && passwd == user[name]["passwd"] {
		return true
	}
	return false

}

//注册
func register(name string, passwd string) bool {
	_, ok := user[name]
	if ok {
		fmt.Println("用户以存在，请重新输入")
		return false
	}else {
		var money int
		fmt.Print("请输入你的金额：")
		fmt.Scan(&money)
		var name_map = map[string]interface{}{
			"passwd":passwd,
			"money":money}
		user[name] = name_map
		fmt.Println("将用户信息写入文件")
		if WriteUserInfo() {
			fmt.Println("用户信息写入成功")
		}else {
			fmt.Println("打开文件，写入用户信息失败！")
		}
		return true
	}
}
//将用户信息写入文件
func WriteUserInfo() bool {
	file, err := os.OpenFile(user_file, os.O_CREATE|os.O_WRONLY,666)
	if err != nil {
		return false
	}else {
		str_m1, _ := json.Marshal(user)
		file.Write(str_m1)
		return true
	}
}

//菜单
func menu() {
	fmt.Println(strings.Repeat("*",20))
	fmt.Println("1、查看商品列表")
	fmt.Println("2、添加到购物车")
	fmt.Println("3、结算")
	fmt.Println("4、退出")
}

//展示商品列表
func show_goods() {
	fmt.Println("编号", strings.Repeat(" ", 10), "名称", strings.Repeat(" ", 10), "单价")
	for k, _ :=range products{
		fmt.Println(k, strings.Repeat(" ", 9), products[k]["name"], strings.Repeat(" ", 10), products[k]["price"])
	}
}

//购物车
func add_car(name string, product_id string, num string)  {
	value := []string{product_id, num}
	car[name] = append(car[name], value)
	fmt.Println(car)
}

//结算
func checkout(name string)  {
	m1 := int(user[name]["money"].(float64))
	car_list := car[name]
	total := 0
	fmt.Println("编号  ", "名称  ", "单价  ", "数量  ", "总价")
	for _, goods := range car_list {
		goods_name := products[goods[0]]["name"]
		price := products[goods[0]]["price"].(int)
		num,_ := strconv.Atoi(goods[1])
		goods_total := price * num
		total += goods_total
		fmt.Println(goods[0], " ", goods_name, " ", price, " ", goods[1], " ", goods_total)
	}
	fmt.Println("共计：", total)
	if m1 >= total {
		m1 -= total
	}else {
		fmt.Println("余额不足！")
	}
	user[name]["money"] = m1
	WriteUserInfo()
	fmt.Println("账户余额：", user[name]["money"])
}